package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.CouponTpl;
import com.qianniu.napi.admin.entity.Czfan;
import com.qianniu.napi.admin.entity.Fenxiao;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.jsmodel.Huiyuan;
import com.qianniu.napi.admin.mapper.CzfanMapper;
import com.qianniu.napi.admin.service.ICouponTplService;
import com.qianniu.napi.admin.service.ICzfanService;
import com.qianniu.napi.admin.service.IFenxiaoService;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.commons.lang.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 充值返现|积分
 * </p>
 *
 * @author chenxuebin
 * @since 2019-03-18
 */
@Controller
public class CzfanController {

    private static final Logger logger = LoggerFactory.getLogger(CzfanController.class);

    @Autowired
    private ICzfanService czfanService;
    @Autowired
    private CzfanMapper czfanMapper;
    @Autowired
    private IFenxiaoService fenxiaoService;
    @Autowired
    private ICouponTplService couponTplService;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("admin/czfan/index")
    public String index(Model model) {

        Fenxiao fenxiao = fenxiaoService.selectOne(new EntityWrapper<Fenxiao>().eq("mpid", ObjectUtil.getMpid()));
        List<Huiyuan> huiyuans = new ArrayList<Huiyuan>();
        if (fenxiao != null) {
            model.addAttribute("hylevel", fenxiao.getHylevel());
            logger.info("============================================================fenxiao="+ JSON.toJSONString(fenxiao));
            for (int i = 1; i <= fenxiao.getHylevel(); i++) {
                Huiyuan hy = new Huiyuan();
                if (i == 1) {
                    hy.setName(fenxiao.getFxname1());
                    hy.setHuifei(fenxiao.getHuifei1());
                    hy.setLevel(1);
                }else if (i == 2) {
                    hy.setName(fenxiao.getFxname2());
                    hy.setHuifei(fenxiao.getHuifei2());
                    hy.setLevel(2);
                }else if (i == 3) {
                    hy.setName(fenxiao.getFxname3());
                    hy.setHuifei(fenxiao.getHuifei3());
                    hy.setLevel(3);
                }else if (i == 4) {
                    hy.setName(fenxiao.getFxname4());
                    hy.setHuifei(fenxiao.getHuifei4());
                    hy.setLevel(4);
                }else if (i == 5) {
                    hy.setName(fenxiao.getFxname5());
                    hy.setHuifei(fenxiao.getHuifei5());
                    hy.setLevel(5);
                }else if (i == 6) {
                    hy.setName(fenxiao.getFxname6());
                    hy.setHuifei(fenxiao.getHuifei6());
                    hy.setLevel(6);
                }else if (i == 7) {
                    hy.setName(fenxiao.getFxname7());
                    hy.setHuifei(fenxiao.getHuifei7());
                    hy.setLevel(7);
                }
                huiyuans.add(hy);
            }

        } else {
            model.addAttribute("hylevel", 0);
        }
        logger.info("============================================================huiyuans="+ JSON.toJSONString(huiyuans));
        model.addAttribute("huiyuans", huiyuans);
        model.addAttribute("fenxiao", fenxiao);

        List<CouponTpl> couponTpls = couponTplService.getMechantCouponstpl(ObjectUtil.getMpid());

        List<Goods> goods = goodsService.selectList(new EntityWrapper<Goods>()
                .eq("mpid", ObjectUtil.getMpid())
                .eq("state", 1)
                .orderBy("createtime", false)
        );

        model.addAttribute("couponTpls", couponTpls);
        model.addAttribute("goods", goods);


        return "admin/czfan";
    }

    @RequestMapping("/admin/czfan/list")
    @ResponseBody
    public Page<Czfan> list(@RequestParam(value = "offset", required = false) Integer offset,
                            @RequestParam(value = "status", required = false) Integer status,
                            @RequestParam(value = "search", required = false) String search,
                            @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Czfan> wrapper = new EntityWrapper<Czfan>();
        if(ObjectUtil.getMpid()==null|| Strings.isNullOrEmpty(ObjectUtil.getMpid().toString()))
        wrapper.eq("mid", ObjectUtil.getMid());
        else
            wrapper.eq("mpid", ObjectUtil.getMpid());

//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("id", false);
        Page<Czfan> page = czfanService.selectPage(new Page<Czfan>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/czfan/add")
    public String add(@Valid Czfan bean,  Model model) {

        if(bean.getQuanshu()==0){
            bean.setQuanshu(null);
        }
        if(bean.getGoodsshu()==0){
            bean.setGoodsshu(null);
        }
        logger.debug("=============czfan/add bean"+bean);

        Fenxiao fenxiao = fenxiaoService.selectOne(new EntityWrapper<Fenxiao>()
                .eq("mpid",ObjectUtil.getMpid())
                .eq("hytype",1)
        );
        logger.debug("=============czfan/add bean" + JSON.toJSONString(bean));
        bean.setDiscount(bean.getLevel()==1?fenxiao.getMzhekou1():
                bean.getLevel()==2?fenxiao.getMzhekou2():
                        bean.getLevel()==3?fenxiao.getMzhekou3():
                                bean.getLevel()==4?fenxiao.getMzhekou4():
                                        bean.getLevel()==5?fenxiao.getMzhekou5():
                                                bean.getLevel()==6?fenxiao.getMzhekou6():
                                                        bean.getLevel()==7?fenxiao.getMzhekou7():new BigDecimal(100));

        bean.setState(1);
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setUid(ObjectUtil.getUid());
        if(bean.getFtype()==1){//积分
//            bean.setFanjifen(null);
            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setFanbi(null);
            bean.setFangoods(null);
        }else if(bean.getFtype()==2){//现金
            bean.setFanjifen(null);
//            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setFanbi(null);
            bean.setFangoods(null);
        }else if(bean.getFtype()==3){//优惠券
            bean.setFanjifen(null);
            bean.setFancash(null);
//            bean.setFanquan(null);
            bean.setFanbi(null);
            bean.setFangoods(null);
        }else if(bean.getFtype()==4){//虚拟币
            bean.setFanjifen(null);
            bean.setFancash(null);
            bean.setFanquan(null);
//            bean.setFanbi(null);
            bean.setFangoods(null);
        }else if(bean.getFtype()==5){//商品
            bean.setFanjifen(null);
            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setFanbi(null);
//            bean.setFangoods(null);
        }else if(bean.getFtype()==6){//混合
//            bean.setFanjifen(null);
//            bean.setFancash(null);
//            bean.setFanquan(null);
//            bean.setFanbi(null);
//            bean.setFangoods(null);
        }
        bean.insert();

        List<Huiyuan> huiyuans = new ArrayList<Huiyuan>();

        if (fenxiao != null) {
            model.addAttribute("hylevel", fenxiao.getHylevel());
            for (int i = 1; i <= fenxiao.getHylevel(); i++) {
                Huiyuan hy = new Huiyuan();
                if (i == 1) {
                    hy.setName(fenxiao.getFxname1());
                    hy.setHuifei(fenxiao.getHuifei1());
                    hy.setDiscount(fenxiao.getMzhekou1());
                    hy.setLevel(1);
                }else if (i == 2) {
                    hy.setName(fenxiao.getFxname2());
                    hy.setHuifei(fenxiao.getHuifei2());
                    hy.setDiscount(fenxiao.getMzhekou2());
                    hy.setLevel(2);
                }else if (i == 3) {
                    hy.setName(fenxiao.getFxname3());
                    hy.setHuifei(fenxiao.getHuifei3());
                    hy.setDiscount(fenxiao.getMzhekou3());
                    hy.setLevel(3);
                }else if (i == 4) {
                    hy.setName(fenxiao.getFxname4());
                    hy.setHuifei(fenxiao.getHuifei4());
                    hy.setDiscount(fenxiao.getMzhekou4());
                    hy.setLevel(4);
                }else if (i == 5) {
                    hy.setName(fenxiao.getFxname5());
                    hy.setHuifei(fenxiao.getHuifei5());
                    hy.setDiscount(fenxiao.getMzhekou5());
                    hy.setLevel(5);
                }else if (i == 6) {
                    hy.setName(fenxiao.getFxname6());
                    hy.setHuifei(fenxiao.getHuifei6());
                    hy.setDiscount(fenxiao.getMzhekou6());
                    hy.setLevel(6);
                }else if (i == 7) {
                    hy.setName(fenxiao.getFxname7());
                    hy.setHuifei(fenxiao.getHuifei7());
                    hy.setDiscount(fenxiao.getMzhekou7());
                    hy.setLevel(7);
                }
                huiyuans.add(hy);
            }
        }
        
        model.addAttribute("huiyuans", huiyuans);
        model.addAttribute("fenxiao", fenxiao);

        List<CouponTpl> coupontpls = couponTplService.getMechantCouponstpl(ObjectUtil.getMpid());
        List<Goods> goods = goodsService.selectList(new EntityWrapper<Goods>()
                .eq("mpid", ObjectUtil.getMpid())
                .eq("state", 1)
                .orderBy("createtime", false)
        );

        model.addAttribute("coupontpls", coupontpls);
        model.addAttribute("goods", goods);
        return "admin/czfan";
    }

    @RequestMapping("/admin/czfan/getCzfanById")
    @ResponseBody
    public Czfan getCzfanById(long id) {
        Czfan czfan = czfanService.selectById(id);
        return czfan;
    }

    @RequestMapping("/admin/czfan/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = czfanService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/czfan/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Czfan bean = new Czfan();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        czfanService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/czfan/modify")
    public String update(Czfan bean) {
        logger.debug("===============Czfan=",bean);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        if(bean.getFtype()==1){//积分
//            bean.setFanjifen(null);
            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setQuanshu(null);
            bean.setFanbi(null);
            bean.setFangoods(null);
            bean.setGoodsshu(null);
        }else if(bean.getFtype()==2){//现金
            bean.setFanjifen(null);
//            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setQuanshu(null);
            bean.setFanbi(null);
            bean.setFangoods(null);
            bean.setGoodsshu(null);
        }else if(bean.getFtype()==3){//优惠券
            bean.setFanjifen(null);
            bean.setFancash(null);
//            bean.setFanquan(null);
            bean.setFanbi(null);
            bean.setFangoods(null);
            bean.setGoodsshu(null);
        }else if(bean.getFtype()==4){//虚拟币
            bean.setFanjifen(null);
            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setQuanshu(null);
//            bean.setFanbi(null);
            bean.setFangoods(null);
            bean.setGoodsshu(null);
        }else if(bean.getFtype()==5){//商品
            bean.setFanjifen(null);
            bean.setFancash(null);
            bean.setFanquan(null);
            bean.setQuanshu(null);
            bean.setFanbi(null);
//            bean.setFangoods(null);
//            bean.setGoodsshu(null);
        }else if(bean.getFtype()==6){//混合
//            bean.setFanjifen(null);
//            bean.setFancash(null);
//            bean.setFanquan(null);
//            bean.setFanbi(null);
//            bean.setFangoods(null);
        }
        boolean isSuccc = czfanService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
